/**
 * 
 */
package com.training1.core.modules.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import scala.collection.generic.BitOperations.Int;

import com.training1.core.common.entity.BOXUserEntity;
import com.training1.core.common.global.signIn.BOXLoginInfoUtil;
import com.training1.core.common.service.CrudService;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.StringUtils;
import com.training1.core.common.utils.excel.ExcelUtil;
import com.training1.core.entity.ContractAttachEntity;
import com.training1.core.entity.ContractBaseEntity;
import com.training1.core.entity.ContractDetEntity;
import com.training1.core.entity.ContractEXEEntity;
import com.training1.core.entity.FirContractEntity;
import com.training1.core.entity.FirSecContractExeEntity;
import com.training1.core.entity.PaymentEntity;
import com.training1.core.entity.RemindEntity;
import com.training1.core.entity.UnderCompanyEntity;
import com.training1.core.modules.dao.ContractAttachDao;
import com.training1.core.modules.dao.ContractDao;
import com.training1.core.modules.dao.ContractDetDao;
import com.training1.core.modules.dao.ContractEXEDao;
import com.training1.core.modules.dao.FirContractDao;
import com.training1.core.modules.dao.FirSecContractExeDao;
import com.training1.core.modules.dao.RemindDao;
import com.training1.core.modules.dao.UserDao;

/**
 * 二审Service
 * @author 何磊
 *
 */
@Service
@Transactional
public class SecContractService extends CrudService<FirContractDao,FirContractEntity>{
	
	@Resource
	private FirContractDao firContractDao;
	@Resource
	private ContractAttachDao contractAttachDao;
	@Resource
	private FirSecContractExeDao firSecContractExeDao;
	@Resource
	private ContractDetDao contractDetDao;
	@Resource
	private ContractEXEDao contractEXEDao;
	@Resource
	private RemindDao remindDao;
	
	
	
	/**
	 * 根据条件查询二审的合同
	 */
	
	public List<FirContractEntity> findAllSecContract(Map<String,Object> map){
		
		List<FirContractEntity> firContractes = firContractDao.findAllSecContract(map);
		
		
		return firContractes;
	}
	
	/**
	 * 根据合同id 查询二审合同的详情
	 */
	
	public FirSecContractExeEntity findContractExeById(Map<String,Object> map){
		
		FirSecContractExeEntity FirContractExe = firSecContractExeDao.findSecContractById(map);
		//查询该合同的所有附件
		List<ContractAttachEntity> contractAttaches = contractAttachDao.findContractAttachEntityById(map);
		
		FirContractExe.setContractAttaches(contractAttaches);
		
		return FirContractExe;
	}
	
	/**
	 * 导出excel
	 * @param response
	 * @param param
	 */
	public void export(HttpServletResponse response,Map<String, Object> param) {
		List<FirContractEntity> pList = firContractDao.findSecExportList(param);//根据dao中的内容用方法
		//设置列标题
		List<String> excelHead = new ArrayList<String>();
		excelHead.add("合同编号");
		excelHead.add("客户姓名");
		excelHead.add("所属公司");
		excelHead.add("客户手机");
		excelHead.add("拟借金额");
		excelHead.add("申请日期");
		excelHead.add("服务期数");
		
		//设置内容
		List<List<String>> dataList = new ArrayList<List<String>>();
		for(FirContractEntity fc : pList){
			List<String> data = new ArrayList<String>();
			
			data.add(fc.getId().toString());
			data.add(fc.getUserName());
			data.add(fc.getCompanyName());
			data.add(fc.getCellphone());
			data.add(fc.getExceptAmount().toString());
			data.add(fc.getAppDateStr());
			data.add(String.valueOf(fc.getCyc()));
			
			
		
			dataList.add(data);
		}
		
		//设置文件名
		String fileName;
		String sheetName = "sheet1";
		String titleName;
		fileName = "合同二审_"+DateUtils.getDate().replace("-", "");
		titleName = "合同二审_"+DateUtils.getDate().replace("-", "");
		
		ExcelUtil.exportExcel(response, excelHead, dataList, fileName, sheetName,titleName);
	}
	
	/**
	 * 二审拒绝后 更新基本合同表
	 * @param map
	 * @return
	 */
	public String refuse(Map<String,Object> map){
		
		firSecContractExeDao.updateSecContractRefuse(map);
		
		
		return "1";
		
	}
	/**
	 * 二审同意后 更新基本合同表
	 * @param map
	 * @return
	 */
	public String agree(Map<String,Object> map){
		
		firSecContractExeDao.updateSecContractAgree(map);
		
		//生成合同执行详情
		FirSecContractExeEntity firSecContractExeEntity = firSecContractExeDao.findContractById(map);
		String cusotmerId = firSecContractExeEntity.getCustomerId();//客户id
		String customerName = firSecContractExeEntity.getCustomerName();//客户姓名
		Date secInsDate = firSecContractExeEntity.getSecInsDate();//二审日期
		BigDecimal approvalAmount = firSecContractExeEntity.getApprovalAmount();//审批金额
		BigDecimal serFee =new BigDecimal(Integer.toString(firSecContractExeEntity.getSerFee())) ;//服务费率
		BigDecimal approvalCyc =new BigDecimal(Integer.toString(firSecContractExeEntity.getApprovalCyc())) ;//审批期数、
		BigDecimal totalInterest = approvalAmount.multiply(serFee).multiply(approvalCyc).
				multiply(new BigDecimal("7")).divide(new BigDecimal("3"),2,BigDecimal.ROUND_UP);//总利息
		
		BigDecimal totalRepayAmount = totalInterest.add(approvalAmount).divide(approvalCyc,2,BigDecimal.ROUND_UP);//还款总额
		//按照审批期数  循环生成合同执行假话
		for(int i = 1;i <= approvalCyc.intValue();i++){
			ContractEXEEntity  contractEXEEntity = new ContractEXEEntity();
			contractEXEEntity.setContractbaseid(map.get("id").toString());
			contractEXEEntity.setCyc(i);
			contractEXEEntity.setExpRepayDate(new Date(secInsDate.getTime() - (long)7 * 24 * 60 * 60 * 1000*i));;
			contractEXEEntity.setExpRepayAmount(totalRepayAmount);
			contractEXEEntity.setPrincipal(approvalAmount.divide(approvalCyc,2,BigDecimal.ROUND_UP));
			contractEXEEntity.setInterest(totalInterest.divide(approvalCyc,2,BigDecimal.ROUND_UP));
			contractEXEEntity.setSurplusPrin(approvalAmount.subtract(approvalAmount.divide(approvalCyc,2,BigDecimal.ROUND_UP).multiply(new BigDecimal(Integer.toString(i)))));
			contractEXEEntity.setTotalPrin(approvalAmount.divide(approvalCyc,2,BigDecimal.ROUND_UP).multiply(new BigDecimal(Integer.toString(i))));
			contractEXEEntity.setTotalInterest(approvalAmount.divide(approvalCyc,2,BigDecimal.ROUND_UP).multiply(new BigDecimal(Integer.toString(i))));
			
			contractEXEDao.save(contractEXEEntity);
			
			//保存至合同提醒表
			RemindEntity remind =  new RemindEntity();
			String remMemo = customerName+"你好，请在明天"+contractEXEEntity.getExpDate()+"日还款"+totalRepayAmount.toString()+"元";
			remind.setCustomerId(cusotmerId);
			remind.setRemMemo(remMemo);
			remind.setExpRemDate(new Date(contractEXEEntity.getExpRepayDate().getTime() - (long)1 * 24 * 60 * 60 * 1000));
			remind.setActRemDate(contractEXEEntity.getExpRepayDate());
			
			remindDao.save(remind);
			
			
		}
		
		
		
		
		return "1";
		
	}
	
}
